<?php

/**
 * This is the model class for table "client".
 *
 * The followings are the available columns in table 'client':
 * @property integer $id
 * @property string $client_lastname
 * @property string $client_firstname
 * @property string $client_midname
 * @property string $gender
 * @property string $birthdate
 * @property string $birthplace
 * @property string $phonenum
 * @property string $mobilenum
 * @property string $business_phone
 * @property string $address
 * @property string $zipcode
 * @property string $city_province
 * @property string $marital_status
 * @property string $occupation
 * @property string $monthly_income
 * @property integer $isAssured
 * @property integer $employee_id
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property Employee $employee
 * @property User $user
 * @property Insurance[] $insurances
 */
class Client extends CActiveRecord
{
	
	public function getNames()
	{
		return $this->client_firstname.' '. $this->client_lastname;
	}
	
	public function getEmployees()
	{
		return CHtml::listData(Employee::model()->findAll(), 'id', 'Names');
	}
	
	//gender
	const G='';
	const F='F';
	const M='M';
	
	public function getGender()
	{
		return array(
			self::G=>'Select Gender',
			self::F=>'Female',
			self::M=>'Male',
		);
	}
	
	//marital status
	const MS='';
	const MR='married';
	const D='divorced';
	const SP='separated';
	const W='widowed';
	const S='single';
	
	public function getMaritalStatus()
	{
		return array(
			self::MS=>'Select Marital Status',
			self::MR=>'Married',
			self::D=>'Divorced',
			self::SP=>'Separated',
			self::W=>'Widowed',
			self::S=>'Single',
		);
	}
	
	//Application Status
	const P='';
	const Y='1';
	const N='0';
	
	public function getIsAssured()
	{
		return array(
			self::P=>'Pending',
			self::Y=>'Approved',
			self::N=>'Denied',
		);
	}
		
	public function getClients()
	{
		return CHtml::listData(Client::model()->findAll(), 'id', 'Names');
	}
	
	public function getInsuranceCo()
	{
		return CHtml::listData(InsuranceCompany::model()->findAll(), 'id', 'company_name');
	}
	
	public function getInsuranceType()
	{
		return CHtml::listData(InsuranceType::model()->findAll(), 'id', 'type_name');
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Client the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'client';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('client_lastname, client_firstname, gender, birthdate, address, marital_status, occupation, employee_id, user_id', 'required'),
			array('isAssured, employee_id, user_id', 'numerical', 'integerOnly'=>true),
			array('client_lastname, client_firstname, client_midname', 'length', 'max'=>100),
			//array('gender', 'length', 'max'=>1),
			array('birthplace, address, city_province, marital_status, monthly_income', 'length', 'max'=>45),
			array('phonenum, business_phone', 'length', 'max'=>10),
			array('mobilenum', 'length', 'min'=>13, 'max'=>13),
			array('zipcode', 'length', 'min'=>4, 'max'=>4),
			array('occupation', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, client_lastname, client_firstname, client_midname, gender, birthdate, birthplace, phonenum, mobilenum, business_phone, address, zipcode, city_province, marital_status, occupation, monthly_income, isAssured, employee_id, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'employee' => array(self::BELONGS_TO, 'Employee', 'employee_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'insurances' => array(self::HAS_MANY, 'Insurance', 'client_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'client_lastname' => 'Last Name',
			'client_firstname' => 'First Name',
			'client_midname' => 'Middle Name',
			'gender' => 'Gender',
			'birthdate' => 'Birthdate',
			'birthplace' => 'Birth Place',
			'phonenum' => 'Phone Number',
			'mobilenum' => 'Mobile Number',
			'business_phone' => 'Business Phone',
			'address' => 'Address',
			'zipcode' => 'Zip Code',
			'city_province' => 'City/Province',
			'marital_status' => 'Marital Status',
			'occupation' => 'Occupation',
			'monthly_income' => 'Monthly Income',
			'isAssured' => 'Application Status',
			'employee_id' => 'Referred by',
			'user_id' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('client_lastname',$this->client_lastname,true);
		$criteria->compare('client_firstname',$this->client_firstname,true);
		$criteria->compare('client_midname',$this->client_midname,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('birthdate',$this->birthdate,true);
		$criteria->compare('birthplace',$this->birthplace,true);
		$criteria->compare('phonenum',$this->phonenum,true);
		$criteria->compare('mobilenum',$this->mobilenum,true);
		$criteria->compare('business_phone',$this->business_phone,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('zipcode',$this->zipcode,true);
		$criteria->compare('city_province',$this->city_province,true);
		$criteria->compare('marital_status',$this->marital_status,true);
		$criteria->compare('occupation',$this->occupation,true);
		$criteria->compare('monthly_income',$this->monthly_income,true);
		$criteria->compare('isAssured',$this->isAssured);
		$criteria->compare('employee_id',$this->employee_id);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}